package com.whsxt.sys.controller;


import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.whsxt.common.CodeMsg;
import com.whsxt.common.PageInfo;
import com.whsxt.common.Result;
import com.whsxt.common.util.WebContextUtil;
import com.whsxt.sys.data.NoticeData;
import com.whsxt.sys.domain.Notice;
import com.whsxt.sys.domain.User;
import com.whsxt.sys.service.INoticeService;
import cn.hutool.core.util.StrUtil;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 贺炯
 * @since 2019-09-21
 */
@RestController
@RequestMapping("/sys/notice")
public class NoticeController {
	
	@Autowired
	private INoticeService iNoticeService;
	/**
	 * 列表查询
	 * @param data
	 * @return
	 */
	@RequestMapping("/list")
	public Object list(NoticeData data) {
		IPage<Notice> page = new Page<Notice>(data.getPage(),data.getLimit());
		QueryWrapper<Notice> queryWrapper = new QueryWrapper<Notice>();
		queryWrapper.like(StrUtil.isNotBlank(data.getTitle()), "title", data.getTitle());
		queryWrapper.like(StrUtil.isNotBlank(data.getOpername()), "opername",data.getOpername());
		queryWrapper.gt(StrUtil.isNotBlank(data.getStartTime()), "createtime", data.getCreatetime());
		queryWrapper.lt(StrUtil.isNotBlank(data.getEndTime()),"createtime" ,data.getEndTime());
		iNoticeService.page(page, queryWrapper);
		PageInfo pageInfo = new PageInfo(page.getTotal(), page.getRecords());
		return new Result(pageInfo);
	}
	/**
	 * 	新增公告
	 * @return
	 */
	@RequestMapping("/add")
	public Object add(NoticeData data) {
		User user = (User) WebContextUtil.getSession().getAttribute("currUser");
		data.setOpername(user.getName());
		data.setCreatetime(new Date());
		try {
			iNoticeService.save(data);
			return new Result(CodeMsg.NOTICE_ADD_SECCUSS);
		} catch (Exception e) {
			e.printStackTrace();
			return new Result(CodeMsg.NOTICE_ADD_ERROR);
		}
	}
	/**
	 * 批量删除
	 * @param ids
	 * @return
	 */
	@RequestMapping("/batchDelete")
	public Object batchDelete(Integer [] ids) {
		System.out.println(ids);
		Collection<Serializable> idList = new ArrayList<Serializable>();
		for (Integer id : ids) {
			idList.add(id);
		}
		try {
			iNoticeService.removeByIds(idList);
			return new Result(CodeMsg.NOTICE_BATCH_DELETE_SECCUSS);
		} catch (Exception e) {
			e.printStackTrace();
			return new Result(CodeMsg.NOTICE_BATCH_DELETE_ERROE);
		}
	}
	/**
	 * 	删除单个数据
	 * @param id
	 * @return
	 */
	@RequestMapping("/deleteOne")
	public Object deleteOne(Integer id) {
		try {
			iNoticeService.removeById(id);
			return new Result(CodeMsg.NOTICE_DELETE_SECCUSS);
		} catch (Exception e) {
			e.printStackTrace();
			return new Result(CodeMsg.NOTICE_DELETE_ERROE);
		}	
	}
	/**
	 * 	更新单个数据
	 * @param data
	 * @return
	 */
	@RequestMapping("/update")
	public Object update(NoticeData data) {
		try {
			iNoticeService.updateById(data);
			return new Result(CodeMsg.NOTICE_UPDATE_SECCUSS);
		} catch (Exception e) {
			e.printStackTrace();
			return new Result(CodeMsg.NOTICE_UPDATE_ERROE);
		}
	}
}

